package com.settlement.system.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.job.JobUtils;
import com.settlement.system.model.entity.SysAccessory;
import com.settlement.system.model.entity.SysBillSummary;
import com.settlement.system.model.query.BillSummaryQuery;
import com.settlement.system.model.vo.SysBillSummaryVO;
import com.settlement.system.service.SysAccessoryService;
import com.settlement.system.service.SysBillSummaryService;
import com.settlement.system.service.SysSubjectsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标准账单汇总 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-08-05
 */
@Tag(name = "标准账单汇总")
@RestController
@RequestMapping("/api/v1/billSummary")
@RequiredArgsConstructor
public class SysBillSummaryController {

    private final SysBillSummaryService sysBillSummaryService;

    private final JobUtils jobUtils;

    private final SysAccessoryService sysAccessoryService;


    @Operation(summary = "标准账单汇总列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/summaryPage")
    public PageResult<?> getBillSummaryPage(@ParameterObject BillSummaryQuery billSummaryQuery) {
        Page<SysBillSummaryVO> billSummaryList = sysBillSummaryService.getPage(billSummaryQuery);
        return PageResult.success(billSummaryList);
    }

    @Operation(summary = "测试生成汇总", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/testGenerateBillSummary")
    public Result<?> testGenerateBillSummary(@ParameterObject BillSummaryQuery billSummaryQuery) {
        jobUtils.testGenerateBillSummary(billSummaryQuery.getBillMonth());
        return Result.success();
    }

    @Operation(summary = "导出凭证", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:billSummary:export')")
    public void export(@ParameterObject BillSummaryQuery billSummaryQuery, HttpServletResponse response) throws Exception {
        String fileName = "凭证.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
        // 所有辅助项
        SysAccessory sysAccessory = sysAccessoryService.findAll().get(0);
        List<String> headTextList = new ArrayList<>();
        headTextList.add("平台渠道");
        headTextList.add("汇总ID");
        headTextList.add("核算账簿编码");
        headTextList.add("凭证类别编码");
        headTextList.add("入账主体");
        headTextList.add("制单人");
        headTextList.add("制单日期");
        headTextList.add("摘要");
        headTextList.add("科目编码");
        headTextList.add("币种");
        headTextList.add("原币借方金额");
        headTextList.add("本币借方金额");
        headTextList.add("原币贷方金额");
        headTextList.add("本币贷方金额");
        headTextList.add("会计期间");
        headTextList.add("凭证类型");
        headTextList.add(sysAccessory.getAccessoryName1());
        headTextList.add(sysAccessory.getAccessoryName2());
        headTextList.add(sysAccessory.getAccessoryName3());
        headTextList.add(sysAccessory.getAccessoryName4());
        headTextList.add(sysAccessory.getAccessoryName5());
        headTextList.add(sysAccessory.getAccessoryName6());
        headTextList.add(sysAccessory.getAccessoryName7());
        headTextList.add(sysAccessory.getAccessoryName8());
        headTextList.add(sysAccessory.getAccessoryName9());
        headTextList.add("流水号");
        List<Map<String, String>> exportVOList = sysBillSummaryService.listSummary(billSummaryQuery);
        // 设置动态头
        List<List<String>> headList = new ArrayList<>();
        List<String> head;
        for (String text : headTextList) {
            head = new ArrayList<>();
            head.add(text);
            headList.add(head);
        }
        // 表格数据
        List<List<String>> dataList = new ArrayList<>();
        for (Map<String, String> stringStringMap : exportVOList) {
            List<String> data = new ArrayList<>();
            for (String text : headTextList) {
                if (stringStringMap.get(text) == null) {
                    data.add("");
                } else {
                    data.add(stringStringMap.get(text));
                }

            }
            dataList.add(data);
        }
        EasyExcel.write(response.getOutputStream()).head(headList).sheet("凭证").doWrite(dataList);
    }
}
